/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.view.actions;

import com.univcasa.biz.AccountDistributer;
import com.univcasa.biz.impl.AccountDistributerImpl;
import com.univcasa.biz.impl.exception.EtudiantNotFoundException;
import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.Criteres;
import com.univcasa.managing.ComptesStudentsManager;
import com.univcasa.managing.impl.ComptesStudentsManagerImpl;
import com.univcasa.view.forms.ajoutDBCompteStdForm;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author i.makhloufi
 */
public class requestInitPassStdAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            {
        String forward = FAILURE ;
        ajoutDBCompteStdForm stdForm = (ajoutDBCompteStdForm) form;

        String naissance = stdForm.getNaissance();
        DateFormat formater = new SimpleDateFormat("dd-MM-yyyy");

        Criteres crit = new Criteres();
        crit.addCritere("cne", stdForm.getCne());
        crit.addCritere("etab", stdForm.getEtab());
        crit.addCritere("cin", stdForm.getCin());

        AccountDistributer searcher = new AccountDistributerImpl();
        CompteEtudiant std;
        try {
            std = searcher.getAccountStdByCriteria(crit);
            Date stdNaissance =formater.parse( naissance );
            
            if ( stdNaissance.compareTo( std.getNaissance() ) == 0 ) {
                request.getSession().setAttribute("apogee", std.getApogee() );
                forward = SUCCESS;
            }
        }
        catch (EtudiantNotFoundException ex) {
            ActionErrors actionErrors = new ActionErrors();
            actionErrors.add("error", new ActionMessage("error.student.notfound") ) ;
            saveErrors(request, actionErrors);
        }
        catch (ParseException ex) {
            ActionErrors actionErrors = new ActionErrors();
            actionErrors.add("error", new ActionMessage("error.date.malsaisie") ) ;
            saveErrors(request, actionErrors);
        }

        return mapping.findForward( forward );
    }
}
